package com.goldwind.user.controller;

import com.goldwind.pojo.User;
import com.goldwind.pojo.Channel;
import com.goldwind.user.service.userService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("User")
public class userController {

    @Autowired
    private userService userService;

    @GetMapping("home")
    public String home(String email, Model model) {
        User user = userService.selectByEmail(email);
        List<Channel> channelList = userService.selectChannel(user.getUserId());
        model.addAttribute("email", email);
        model.addAttribute("user", user);
        Gson gson = new Gson();
        model.addAttribute("channelListJson", gson.toJson(channelList));
        return "User/home";
    }

    @GetMapping("userInformation")
    public String userInfo(String email,Model model) {
        User user = userService.selectByEmail(email);
        model.addAttribute("email", email);
        model.addAttribute("user", user);
        return "User/userInformation";
    }

    @GetMapping("discover")
    public String discover(String email,Model model) {
        User user = userService.selectByEmail(email);
        List<Channel> channelList = userService.selectChannel(user.getUserId());
        List<Channel> suggestChannel = userService.suggestChannel();
        List<Channel> loveChannel =userService.loveChannel(user.getUserId());
        Gson gson = new Gson();
        model.addAttribute("email", email);
        model.addAttribute("suggestChannel", gson.toJson(suggestChannel));
        model.addAttribute("channelListJson", gson.toJson(channelList));
        model.addAttribute("loveChannelJson", gson.toJson(loveChannel));
        model.addAttribute("user", user);
        List<Integer> addedChannel = userService.addedChannels(user.getUserId());
        model.addAttribute("addedChannel", addedChannel);
        return "User/discover";
    }

    @PostMapping("updateUser")
    @ResponseBody
    public Map<String, Object> updateUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            int updateResult = userService.updateUser(user);
            if (updateResult > 0) {
                result.put("success", true);
                result.put("message", "用户信息更新成功");
            } else {
                result.put("success", false);
                result.put("message", "用户信息更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新过程中发生错误: " + e.getMessage());
        }
        return result;
    }

    @PostMapping("createChannel")
    @ResponseBody
    public Map<String, Object> createChannel(@RequestBody Channel channel) {
        Map<String, Object> result = new HashMap<>();
        try {
            int createResult = userService.createChannel(channel);
            int userId = channel.getUserId();
            userService.concat(userId);
            if (createResult > 0) {
                result.put("success", true);
                result.put("message", "频道创建成功");
            } else {
                result.put("success", false);
                result.put("message", "频道创建失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "创建频道过程中发生错误: " + e.getMessage());
        }
        return result;
    }

    @PostMapping("findChannel")
    @ResponseBody
    public List<Channel> findChannel(String sort) {
        List<Channel> channelList = userService.findChannel(sort);
        return channelList;
    }

    @PostMapping("searchChannel")
    @ResponseBody
    public List<Channel> searchChannel(String name) {
        List<Channel> channelList = userService.searchChannel(name);
        return channelList;
    }

    @PostMapping("entryChannel")
    @ResponseBody
    public String entryChannel(int userId,int cId) {
        int num = userService.entryChannel(userId,cId);
        if (num > 0) {
            return "success";
        }
        return "fail";
    }
}
